package com.pig4cloud.pig.djjk.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.djjk.entity.DeviceEntity;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.djjk.entity.TrackAnalysisEntity;
import com.pig4cloud.pig.djjk.service.TrackAnalysisService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 抓拍统计表
 *
 * @author pig
 * @date 2025-06-16 14:26:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/trackAnalysis" )
@Tag(description = "trackAnalysis" , name = "抓拍统计表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TrackAnalysisController {

    private final  TrackAnalysisService trackAnalysisService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param trackAnalysis 抓拍统计表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_view')" )
    public R getTrackAnalysisPage(@ParameterObject Page page, @ParameterObject TrackAnalysisEntity trackAnalysis) {
        LambdaQueryWrapper<TrackAnalysisEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(trackAnalysis.getDeviceCode()),TrackAnalysisEntity::getDeviceCode,trackAnalysis.getDeviceCode());
		wrapper.like(StrUtil.isNotBlank(trackAnalysis.getTheDate()),TrackAnalysisEntity::getTheDate,trackAnalysis.getTheDate());
        return R.ok(trackAnalysisService.page(page, wrapper));
    }


    /**
     * 通过条件查询抓拍统计表
     * @param trackAnalysis 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_view')" )
    public R getDetails(@ParameterObject TrackAnalysisEntity trackAnalysis) {
        return R.ok(trackAnalysisService.list(Wrappers.query(trackAnalysis)));
    }

    /**
     * 新增抓拍统计表
     * @param trackAnalysis 抓拍统计表
     * @return R
     */
    @Operation(summary = "新增抓拍统计表" , description = "新增抓拍统计表" )
    @SysLog("新增抓拍统计表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_add')" )
    public R save(@RequestBody TrackAnalysisEntity trackAnalysis) {
        return R.ok(trackAnalysisService.save(trackAnalysis));
    }

    /**
     * 修改抓拍统计表
     * @param trackAnalysis 抓拍统计表
     * @return R
     */
    @Operation(summary = "修改抓拍统计表" , description = "修改抓拍统计表" )
    @SysLog("修改抓拍统计表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_edit')" )
    public R updateById(@RequestBody TrackAnalysisEntity trackAnalysis) {
        return R.ok(trackAnalysisService.updateById(trackAnalysis));
    }

    /**
     * 通过id删除抓拍统计表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除抓拍统计表" , description = "通过id删除抓拍统计表" )
    @SysLog("通过id删除抓拍统计表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(trackAnalysisService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param trackAnalysis 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_export')" )
    public List<TrackAnalysisEntity> exportExcel(TrackAnalysisEntity trackAnalysis,Long[] ids) {
        return trackAnalysisService.list(Wrappers.lambdaQuery(trackAnalysis).in(ArrayUtil.isNotEmpty(ids), TrackAnalysisEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param trackAnalysisList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('djjk_trackAnalysis_export')" )
    public R importExcel(@RequestExcel List<TrackAnalysisEntity> trackAnalysisList, BindingResult bindingResult) {
        return R.ok(trackAnalysisService.saveBatch(trackAnalysisList));
    }


	@Inner(value = false)
	@RequestMapping("/plusTrackCount" )
	public R plusTrackCount(@RequestBody TrackAnalysisEntity trackAnalysis) {
		Optional<TrackAnalysisEntity> trackAnalysisServiceOneOpt = trackAnalysisService.getOneOpt(new LambdaQueryWrapper<TrackAnalysisEntity>().eq(TrackAnalysisEntity::getDeviceCode, trackAnalysis.getDeviceCode())
				.eq(TrackAnalysisEntity::getTheDate, DateUtil.today()), false);
		if(trackAnalysisServiceOneOpt.isPresent()){
			TrackAnalysisEntity trackAnalysisEntity = trackAnalysisServiceOneOpt.get();
			trackAnalysisEntity.setTrackCount(trackAnalysisEntity.getTrackCount()+1);
			trackAnalysisEntity.setMatchCaount(trackAnalysisEntity.getMatchCaount()+1);
			trackAnalysisEntity.setNoMatchCaount(trackAnalysisEntity.getNoMatchCaount()+1);
			trackAnalysisService.updateById(trackAnalysisEntity);
		}else{
			trackAnalysis.setTrackCount(trackAnalysis.getTrackCount());
			trackAnalysis.setNoMatchCaount(trackAnalysis.getNoMatchCaount());
			trackAnalysis.setMatchCaount(trackAnalysis.getMatchCaount());
			trackAnalysis.setTheDate(DateUtil.today());
			trackAnalysisService.save(trackAnalysis);
		}
		return R.ok();
	}
}
